package gridy.node;

import gridy.client.Client;

import org.apache.tuscany.sca.node.SCAClient;
import org.apache.tuscany.sca.node.SCANode;
import org.apache.tuscany.sca.node.launcher.NodeLauncher;
import org.osoa.sca.ServiceRuntimeException;

public class LaunchClient {
    
    public static void main(String[] args) {
        
        if (args.length != 2 && args.length != 1) {
            System.err.println("Argumenty: [nazwa_hosta_domain_managera] ([port_domain_managera])");
            System.exit(1);
        }        
        String hostname = args[0];
        int port = 9990;
        if (args.length == 2) {
            port = Integer.parseInt(args[1]);
        }
        
        SCANode node = null;
        try {
            
            NodeLauncher nodeLauncher = NodeLauncher.newInstance();
            node = nodeLauncher.createNodeFromURL(String.format("http://%s:%d/node-config/NodeClient",hostname,port));

            node.start();
            
            // get a reference to the calculator component
            SCAClient client = (SCAClient)node;
            Client myClient = 
                client.getService(Client.class, "ClientComponent");
            
            myClient.execute();
            
            node.stop();
            
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }        
    }

}
